import { API_LIST } from '../../../Api/Api'
import { commonTableList, BuiltInData } from '../Common/common'
import {
    AddDataButton,
    ModDataButton,
    DelDataButton,
    OpenTwoRouter,
    ExamineButton,
    SaveExcelButton,
} from '../Common/options'
import { SecurityProblemTypeTree } from '../Common/Conversion'
import { ProjectList } from '../Common/Conversion'

const optionsButton = ['yaxue.ljkjkf.com']

export const Security = async ({ url, OptionsEvent, ClientData, Permission, actionRef }) => {
    const { hostname } = window.location
    const securityProblemTypeUuid = await SecurityProblemTypeTree()
    Permission = Permission.find(v => v.targetUrl === url).children
    const { userUuid } = JSON.parse(sessionStorage.getItem('UserInfo'))

    let data = {
        pageSize: 10,
        params: { pageIndex: 1, OSecurityDate: 'desc@1' },
        get: API_LIST()[`get${url}`],
        added: `pos${url}`,
        modify: `put${url}`,
        delete: `del${url}`,
        options: [
            Permission.findIndex(v => v.uuid === 'f43233b0-e329-4644-96d7-07e3670216c0') !== -1 && (
                <AddDataButton OptionsEvent={OptionsEvent} url={url} type="primary" icon="add" title="新增安全巡检" />
            ),
        ],
        columns: [
            commonTableList.uuid,
            { ...commonTableList.SearchSort, selectType: 'Security' },
            commonTableList.BeginSecurityDate,
            commonTableList.EndSecurityDate,
            {
                ...commonTableList.projectUuid,
                valueEnum: await ProjectList(),
                hideInAdded: true,
                hideInModify: true,
                hideInTable: true,
                hideInSearch: !hostname.includes('jszx'),
            },
            { ...commonTableList.organizationUuid, valueEnum: ClientData.organization, title: '整改人' },
            { ...commonTableList.tableName, title: '巡检主题', width: 250 },
            {
                dataIndex: 'securityProblemTypeUuid',
                title: '安全问题类别',
                valueType: 'treeSelect',
                fieldProps: {
                    options: securityProblemTypeUuid,
                    fieldNames: { label: 'tableName', value: 'uuid' },
                },
                rules: true,
                ellipsis: true,
                hideInSearch: true,
                width: 250,
            },
            {
                dataIndex: 'securityDate',
                title: '巡检日期',
                valueType: 'Date',
                showTime: true,
                rules: true,
                width: 150,
                hideInSearch: true,
            },
            {
                dataIndex: 'limitDays',
                title: '整改天数',
                valueType: 'inputNumber',
                rules: true,
                width: 100,
                hideInSearch: true,
            },
            {
                dataIndex: 'score',
                title: '分数',
                valueType: 'inputNumber',
                rules: true,
                width: 100,
                hideInSearch: true,
            },
            {
                dataIndex: 'auditStatus',
                title: '状态',
                valueType: 'select',
                valueEnum: BuiltInData.PatrolStatus,
                hideInAdded: true,
                hideInModify: true,
                width: 150,
            },
            {
                dataIndex: 'problem',
                title: '问题描述',
                valueType: 'textarea',
                rules: true,
                ellipsis: true,
                width: 400,
                hideInSearch: true,
            },
            { ...commonTableList.createUserName, title: '下单人' },
            commonTableList.createTime,
            commonTableList.updateUserName,
            commonTableList.updateTime,
            {
                title: '操作',
                valueType: 'systemOperation',
                fixed: 'right',
                width: 560,
                hideInSearch: true,
                hideInAdded: true,
                hideInModify: true,
                render: (_, record) => {
                    const { uuid, auditStatus, createUserUuid } = record
                    return [
                        Permission.findIndex(v => v.uuid === 'bfd0660c-ef93-4734-aaca-e410b048f3f7') !== -1 && (
                            <ModDataButton
                                OptionsEvent={OptionsEvent}
                                key="modify"
                                title="修改"
                                url={url}
                                data={record}
                            />
                        ),
                        <OpenTwoRouter
                            key="TwoOne"
                            title="巡检详情"
                            twoTitle="安全巡检问题详情"
                            modal="TableList"
                            OptionsEvent={OptionsEvent}
                            other={{
                                page: 'SecurityProblem',
                                actionRef,
                                Permission,
                                oneData: record,
                                header: data.columns,
                            }}
                        />,

                        auditStatus !== -1 && auditStatus !== 0 && (
                            <OpenTwoRouter
                                key="TwoTwo"
                                title="整改详情"
                                twoTitle="安全巡检整改详情"
                                modal="TableList"
                                OptionsEvent={OptionsEvent}
                                other={{
                                    page: 'SecurityDetails',
                                    actionRef,
                                    Permission,
                                    oneData: record,
                                    header: data.columns,
                                }}
                            />
                        ),

                        createUserUuid === userUuid &&
                            auditStatus === -1 &&
                            Permission.findIndex(v => v.uuid === '85f53e20-49e6-4227-af4b-a3e00b0a4573') !== -1 && (
                                <ExamineButton
                                    key="one"
                                    title="提交问题"
                                    data={{
                                        actionRef,
                                        OptionsEvent,
                                        url: 'patSecuritySubmit',
                                        smallTitle: '提交安全巡检',
                                    }}
                                    options={[
                                        { title: '提交', type: 'primary', emit: { path: [uuid] } },
                                        { title: '取消', type: 'link', emit: 'off' },
                                    ]}
                                />
                            ),
                        auditStatus === 0 &&
                            Permission.findIndex(v => v.uuid === '6e46f4c4-f844-4094-8b0a-bcde0879a256') !== -1 && (
                                <ExamineButton
                                    key="one"
                                    title="接受整改"
                                    data={{
                                        actionRef,
                                        OptionsEvent,
                                        url: 'patSecurityAccept',
                                        smallTitle: '接受安全巡检整改',
                                    }}
                                    options={[
                                        { title: '接受', type: 'primary', emit: { path: [uuid] } },
                                        { title: '取消', type: 'link', emit: 'off' },
                                    ]}
                                />
                            ),
                        auditStatus === 4 &&
                            Permission.findIndex(v => v.uuid === '6e46f4c4-f844-4094-8b0a-bcde0879a256') !== -1 && (
                                <ExamineButton
                                    key="one"
                                    title="重新整改"
                                    data={{
                                        actionRef,
                                        OptionsEvent,
                                        url: 'patSecurityAccept',
                                        smallTitle: '重新接受安全巡检整改',
                                    }}
                                    options={[
                                        { title: '重新接受', type: 'primary', emit: { path: [uuid] } },
                                        { title: '取消', type: 'link', emit: 'off' },
                                    ]}
                                />
                            ),
                        auditStatus === 2 &&
                            Permission.findIndex(v => v.uuid === 'ae37880f-854d-4d77-ab51-40a2c9017512') !== -1 && (
                                <ExamineButton
                                    key="one"
                                    title="审核整改"
                                    data={{
                                        actionRef,
                                        OptionsEvent,
                                        url: 'patSecurityApproval',
                                        smallTitle: '审核已整改的安全巡检',
                                    }}
                                    options={[
                                        { title: '合格', type: 'primary', emit: { path: [uuid, 3] } },
                                        { title: '不合格', type: 'link', danger: true, emit: { path: [uuid, 4] } },
                                    ]}
                                />
                            ),
                        Permission.findIndex(v => v.uuid === '3a93ff0e-cf84-44a2-ab45-26417c83fdb6') !== -1 && (
                            <DelDataButton
                                OptionsEvent={OptionsEvent}
                                key="del"
                                title="删除"
                                url={url}
                                uuid={record.uuid}
                            />
                        ),
                        // 监理回执单
                        optionsButton.includes(hostname) && (
                            <SaveExcelButton
                                key="oneOne"
                                title="监理回执单"
                                // OptionsEvent={OptionsEvent}
                                // data={{
                                //     save: true,
                                //     Uuid: uuid,
                                //     url: 'getSecurityGetSupervisionReceipt',
                                // }}
                                api={API_LIST()['getSecurityGetSupervisionReceipt']}
                                data={{ uuid }}
                            />
                        ),
                        // 监理通知单
                        optionsButton.includes(hostname) && (
                            <SaveExcelButton
                                key="oneTwo"
                                title="监理通知单"
                                // OptionsEvent={OptionsEvent}
                                // data={{
                                //     save: true,
                                //     Uuid: uuid,
                                //     url: 'getSecurityGetSupervisionWotice',
                                // }}
                                api={API_LIST()['getSecurityGetSupervisionWotice']}
                                data={{ uuid }}
                            />
                        ),
                    ]
                },
            },
        ],
    }
    return data
}
